package com.ruoyi.yipaiju.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.XcxUser;
import com.ruoyi.common.core.page.TableDataApi;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.yipaiju.domain.*;
import com.ruoyi.yipaiju.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/bookingorders")
public class BookingordersController extends BaseController {

    @Autowired
    private IForeclosedHouseBookingorderService foreclosedHouseBookingorderService;
    @Autowired
    private IDiscountHouseBookingorderService discountHouseBookingorderService;
    @Autowired
    private IHousePreuploadOrderService housePreuploadOrderService;
    @Autowired
    private IInvestigationOrderService investigationOrderService;
    @Autowired
    private IDemandOrderService demandOrderService;
    @Autowired
    private IValueAddedOrderService valueAddedOrderService;
    @Autowired
    private IXcxUserService xcxUserService;

    /**
     * 全部订单列表
     * @param bookingordersModel
     * @return
     */
    @GetMapping("")
    public TableDataApi list(BookingordersModel bookingordersModel) {
        bookingordersModel.setRouterType("list");
        List<Object> list = queryBookingorders(bookingordersModel);
        return getTableApi(list);
    }

    /**
     * 我的服务 预约列表 0全部 类型1 优选，类型2 折扣，类型3 放盘，类型4 尽调，类型5 好房，类型6 增值
     * @param bookingordersModel
     * @return
     */
    @GetMapping("/me")
    public TableDataApi me(BookingordersModel bookingordersModel) {
        bookingordersModel.setClient(SecurityUtils.getUserId());
        bookingordersModel.setRouterType("me");
        List<Object> list = queryBookingorders(bookingordersModel);
        return getTableApi(list);
    }

    public List<Object> queryBookingorders(BookingordersModel bookingordersModel){
        List<Object> list = new ArrayList<>();
        Integer stage = (bookingordersModel.getStage() == null || bookingordersModel.getStage() == 0) ? null : bookingordersModel.getStage();
        Integer ordertype = bookingordersModel.getOrdertype() == null ? 0 : bookingordersModel.getOrdertype();
        bookingordersModel.setStage(stage);
        bookingordersModel.setOrdertype(ordertype);
        // 0全部 类型1 优选，类型2 折扣，类型3 放盘，类型4 尽调，类型5 好房，类型6 增值
        switch (ordertype){
            case 0:
                list.addAll(bookingordersQueryType1(bookingordersModel));
                list.addAll(bookingordersQueryType2(bookingordersModel));
                list.addAll(bookingordersQueryType3(bookingordersModel));
                list.addAll(bookingordersQueryType4(bookingordersModel));
                list.addAll(bookingordersQueryType5(bookingordersModel));
                list.addAll(bookingordersQueryType6(bookingordersModel));
                break;
            case 1:
//                fo_foreclosed_house_bookingorder
                list.addAll(bookingordersQueryType1(bookingordersModel));
                break;
            case 2:
//                fo_discount_house_bookingorder
                list.addAll(bookingordersQueryType2(bookingordersModel));
                break;
            case 3:
                list.addAll(bookingordersQueryType3(bookingordersModel));
                break;
            case 4:
                list.addAll(bookingordersQueryType4(bookingordersModel));
                break;
            case 5:
//                fo_demand_order
                list.addAll(bookingordersQueryType5(bookingordersModel));
                break;
            case 6:
//                fo_value_added_order
                list.addAll(bookingordersQueryType6(bookingordersModel));
                break;
        }
        return list;
    }


    public List<ForeclosedHouseBookingorder> bookingordersQueryType1(BookingordersModel bookingordersModel){
        startPageApi();
        List<ForeclosedHouseBookingorder> foreclosedHouseSigningorders = foreclosedHouseBookingorderService.selectForeclosedHouseBookingorderList1(Builder.of(ForeclosedHouseBookingorder::new)
                .with(ForeclosedHouseBookingorder::setClient, bookingordersModel.getClient())
                .with(ForeclosedHouseBookingorder::setStage, bookingordersModel.getStage())
                .with(ForeclosedHouseBookingorder::setProvince, bookingordersModel.getProvince())
                .with(ForeclosedHouseBookingorder::setCity, bookingordersModel.getCity())
                .with(ForeclosedHouseBookingorder::setDistrict, bookingordersModel.getDistrict())
                .with(ForeclosedHouseBookingorder::setCreateDate, bookingordersModel.getCreateTime())
                .with(ForeclosedHouseBookingorder::setOrderings, bookingordersModel.getOrderings())
                .build());
        // TODO 查询用户 添加用户头像昵称手机号  原来是前台搞的 现在后台搞
        List<Long> ids = new ArrayList<>();
        if ("me".equals(bookingordersModel.getRouterType())){
            ids = foreclosedHouseSigningorders.stream().filter(item -> item.getMarketer() !=null).map(item -> item.getMarketer()).distinct().collect(Collectors.toList());
        }else if ("list".equals(bookingordersModel.getRouterType())){
            ids = foreclosedHouseSigningorders.stream().filter(item -> item.getClient() !=null).map(item -> item.getClient()).distinct().collect(Collectors.toList());
        }

        Map<Long, XcxUser> xcxUserMap = xcxUserService.getXcxUserByIds(ids);
        foreclosedHouseSigningorders.stream().forEach(item -> {
            Long userId = null;
            if ("me".equals(bookingordersModel.getRouterType())){
                userId = item.getMarketer();
            }else if ("list".equals(bookingordersModel.getRouterType())){
                userId = item.getClient();
            }
            if (userId != null){
                XcxUser xcxUser = xcxUserMap.get(userId);
                if (xcxUser != null){
                    item.setImg(xcxUser.getAvatarUrl());
                    item.setMarkName(xcxUser.getNickname());
                    item.setTelphone(xcxUser.getPhone());
                }
            }
            item.setType(1);
        });
        return foreclosedHouseSigningorders;
    }

    public List<DiscountHouseBookingorder> bookingordersQueryType2(BookingordersModel bookingordersModel){
        startPageApi();
        List<DiscountHouseBookingorder> discountHouseBookingorders = discountHouseBookingorderService.selectDiscountHouseBookingorderList1(Builder.of(DiscountHouseBookingorder::new)
                .with(DiscountHouseBookingorder::setClient, bookingordersModel.getClient())
                .with(DiscountHouseBookingorder::setStage, bookingordersModel.getStage())
                .with(DiscountHouseBookingorder::setProvince, bookingordersModel.getProvince())
                .with(DiscountHouseBookingorder::setCity, bookingordersModel.getCity())
                .with(DiscountHouseBookingorder::setDistrict, bookingordersModel.getDistrict())
                .with(DiscountHouseBookingorder::setCreateDate, bookingordersModel.getCreateTime())
                .with(DiscountHouseBookingorder::setOrderings, bookingordersModel.getOrderings())
                .build());
        List<Long> ids = new ArrayList<>();
        if ("me".equals(bookingordersModel.getRouterType())){
            ids = discountHouseBookingorders.stream().filter(item -> item.getMarketer() !=null).map(item -> item.getMarketer()).distinct().collect(Collectors.toList());
        }else if ("list".equals(bookingordersModel.getRouterType())){
            ids = discountHouseBookingorders.stream().filter(item -> item.getClient() !=null).map(item -> item.getClient()).distinct().collect(Collectors.toList());
        }

        Map<Long, XcxUser> xcxUserMap = xcxUserService.getXcxUserByIds(ids);
        discountHouseBookingorders.stream().forEach(item -> {
            Long userId = null;
            if ("me".equals(bookingordersModel.getRouterType())){
                userId = item.getMarketer();
            }else if ("list".equals(bookingordersModel.getRouterType())){
                userId = item.getClient();
            }
            if (userId != null){
                XcxUser xcxUser = xcxUserMap.get(userId);
                if (xcxUser != null){
                    item.setImg(xcxUser.getAvatarUrl());
                    item.setMarkName(xcxUser.getNickname());
                    item.setTelphone(xcxUser.getPhone());
                }
            }
            item.setType(2);
        });
        return discountHouseBookingorders;
    }

    public List<HousePreuploadOrder> bookingordersQueryType3(BookingordersModel bookingordersModel){
        startPageApi();
        List<HousePreuploadOrder> housePreuploadOrders = housePreuploadOrderService.selectHousePreuploadOrderList1(Builder.of(HousePreuploadOrder::new)
                .with(HousePreuploadOrder::setClient, bookingordersModel.getClient())
                .with(HousePreuploadOrder::setStage, bookingordersModel.getStage())
                .with(HousePreuploadOrder::setProvince, bookingordersModel.getProvince())
                .with(HousePreuploadOrder::setCity, bookingordersModel.getCity())
                .with(HousePreuploadOrder::setDistrict, bookingordersModel.getDistrict())
                .with(HousePreuploadOrder::setCreateDate, bookingordersModel.getCreateTime())
                .with(HousePreuploadOrder::setOrderings, bookingordersModel.getOrderings())
                .build());
        List<Long> ids = new ArrayList<>();
        if ("me".equals(bookingordersModel.getRouterType())){
            ids = housePreuploadOrders.stream().filter(item -> item.getMarketer() !=null).map(item -> item.getMarketer()).distinct().collect(Collectors.toList());
        }else if ("list".equals(bookingordersModel.getRouterType())){
            ids = housePreuploadOrders.stream().filter(item -> item.getClient() !=null).map(item -> item.getClient()).distinct().collect(Collectors.toList());
        }

        Map<Long, XcxUser> xcxUserMap = xcxUserService.getXcxUserByIds(ids);
        housePreuploadOrders.stream().forEach(item -> {
            Long userId = null;
            if ("me".equals(bookingordersModel.getRouterType())){
                userId = item.getMarketer();
            }else if ("list".equals(bookingordersModel.getRouterType())){
                userId = item.getClient();
            }
            if (userId != null){
                XcxUser xcxUser = xcxUserMap.get(userId);
                if (xcxUser != null){
                    item.setImg(xcxUser.getAvatarUrl());
                    item.setMarkName(xcxUser.getNickname());
                    item.setTelphone(xcxUser.getPhone());
                }
            }
            item.setType(3);
        });
        return housePreuploadOrders;
    }

    public List<InvestigationOrder> bookingordersQueryType4(BookingordersModel bookingordersModel){
        startPageApi();
        List<InvestigationOrder> investigationOrders = investigationOrderService.selectInvestigationOrderList1(Builder.of(InvestigationOrder::new)
                .with(InvestigationOrder::setClient, bookingordersModel.getClient())
                .with(InvestigationOrder::setStage, bookingordersModel.getStage())
                .with(InvestigationOrder::setProvince, bookingordersModel.getProvince())
                .with(InvestigationOrder::setCity, bookingordersModel.getCity())
                .with(InvestigationOrder::setDistrict, bookingordersModel.getDistrict())
                .with(InvestigationOrder::setCreateDate, bookingordersModel.getCreateTime())
                .with(InvestigationOrder::setOrderings, bookingordersModel.getOrderings())
                .build());
        List<Long> ids = new ArrayList<>();
        if ("me".equals(bookingordersModel.getRouterType())){
            ids = investigationOrders.stream().filter(item -> item.getMarketer() !=null).map(item -> item.getMarketer()).distinct().collect(Collectors.toList());
        }else if ("list".equals(bookingordersModel.getRouterType())){
            ids = investigationOrders.stream().filter(item -> item.getClient() !=null).map(item -> item.getClient()).distinct().collect(Collectors.toList());
        }

        Map<Long, XcxUser> xcxUserMap = xcxUserService.getXcxUserByIds(ids);
        investigationOrders.stream().forEach(item -> {
            Long userId = null;
            if ("me".equals(bookingordersModel.getRouterType())){
                userId = item.getMarketer();
            }else if ("list".equals(bookingordersModel.getRouterType())){
                userId = item.getClient();
            }
            if (userId != null){
                XcxUser xcxUser = xcxUserMap.get(userId);
                if (xcxUser != null){
                    item.setImg(xcxUser.getAvatarUrl());
                    item.setMarkName(xcxUser.getNickname());
                    item.setTelphone(xcxUser.getPhone());
                }
            }
            item.setType(4);
        });
        return investigationOrders;
    }

    public List<DemandOrder> bookingordersQueryType5(BookingordersModel bookingordersModel){
        startPageApi();
        List<DemandOrder> demandOrders = demandOrderService.selectDemandOrderList1(Builder.of(DemandOrder::new)
                .with(DemandOrder::setClient, bookingordersModel.getClient())
                .with(DemandOrder::setStage, bookingordersModel.getStage())
                .with(DemandOrder::setProvince, bookingordersModel.getProvince())
                .with(DemandOrder::setCity, bookingordersModel.getCity())
                .with(DemandOrder::setDistrict, bookingordersModel.getDistrict())
                .with(DemandOrder::setCreateDate, bookingordersModel.getCreateTime())
                .with(DemandOrder::setOrderings, bookingordersModel.getOrderings())
                .build());
        List<Long> ids = new ArrayList<>();
        if ("me".equals(bookingordersModel.getRouterType())){
            ids = demandOrders.stream().filter(item -> item.getMarketer() !=null).map(item -> item.getMarketer()).distinct().collect(Collectors.toList());
        }else if ("list".equals(bookingordersModel.getRouterType())){
            ids = demandOrders.stream().filter(item -> item.getClient() !=null).map(item -> item.getClient()).distinct().collect(Collectors.toList());
        }

        Map<Long, XcxUser> xcxUserMap = xcxUserService.getXcxUserByIds(ids);
        demandOrders.stream().forEach(item -> {
            Long userId = null;
            if ("me".equals(bookingordersModel.getRouterType())){
                userId = item.getMarketer();
            }else if ("list".equals(bookingordersModel.getRouterType())){
                userId = item.getClient();
            }
            if (userId != null){
                XcxUser xcxUser = xcxUserMap.get(userId);
                if (xcxUser != null){
                    item.setImg(xcxUser.getAvatarUrl());
                    item.setMarkName(xcxUser.getNickname());
                    item.setTelphone(xcxUser.getPhone());
                }
            }
            item.setType(5);
        });
        return demandOrders;
    }

    public List<ValueAddedOrder> bookingordersQueryType6(BookingordersModel bookingordersModel){
        startPageApi();
        List<ValueAddedOrder> valueAddedOrders = valueAddedOrderService.selectValueAddedOrderList1(Builder.of(ValueAddedOrder::new)
                .with(ValueAddedOrder::setClient, bookingordersModel.getClient())
                .with(ValueAddedOrder::setStage, bookingordersModel.getStage())
                .with(ValueAddedOrder::setProvince, bookingordersModel.getProvince())
                .with(ValueAddedOrder::setCity, bookingordersModel.getCity())
                .with(ValueAddedOrder::setDistrict, bookingordersModel.getDistrict())
                .with(ValueAddedOrder::setCreateDate, bookingordersModel.getCreateTime())
                .with(ValueAddedOrder::setOrderings, bookingordersModel.getOrderings())
                .build());
        List<Long> ids = new ArrayList<>();
        if ("me".equals(bookingordersModel.getRouterType())){
            ids = valueAddedOrders.stream().filter(item -> item.getMarketer() !=null).map(item -> item.getMarketer()).distinct().collect(Collectors.toList());
        }else if ("list".equals(bookingordersModel.getRouterType())){
            ids = valueAddedOrders.stream().filter(item -> item.getClient() !=null).map(item -> item.getClient()).distinct().collect(Collectors.toList());
        }

        Map<Long, XcxUser> xcxUserMap = xcxUserService.getXcxUserByIds(ids);
        valueAddedOrders.stream().forEach(item -> {
            Long userId = null;
            if ("me".equals(bookingordersModel.getRouterType())){
                userId = item.getMarketer();
            }else if ("list".equals(bookingordersModel.getRouterType())){
                userId = item.getClient();
            }
            if (userId != null){
                XcxUser xcxUser = xcxUserMap.get(userId);
                if (xcxUser != null){
                    item.setImg(xcxUser.getAvatarUrl());
                    item.setMarkName(xcxUser.getNickname());
                    item.setTelphone(xcxUser.getPhone());
                }
            }
            item.setType(6);
        });
        return valueAddedOrders;
    }

}
